// GalaxyScreenView.cs
//
// Copyright (c) 2009 Mike Strobel
//
// This source code is subject to the terms of the Microsoft Reciprocal License (Ms-RL).
// For details, see <http://www.opensource.org/licenses/ms-rl.html>.
//
// All other rights reserved.

using System.Windows;
using System.Windows.Input;

using Microsoft.Practices.Unity;

using Supremacy.Annotations;
using Supremacy.Client.Commands;

namespace Supremacy.Client.Views
{
    public class GalaxyScreenView : GameScreenView<GalaxyScreenPresentationModel>, IGalaxyScreenView
    {
        static GalaxyScreenView()
        {
            DefaultStyleKeyProperty.OverrideMetadata(
                typeof(GalaxyScreenView),
                new FrameworkPropertyMetadata(typeof(GalaxyScreenView)));
        }

        public GalaxyScreenView([NotNull] IUnityContainer container)
            : base(container)
        {
            this.InputBindings.Add(
                new KeyBinding(
                    ClientCommands.EndTurn,
                    Key.Enter,
                    ModifierKeys.Control));

            this.InputBindings.Add(
                new KeyBinding(
                    ClientCommands.EscapeCommand,
                    Key.Escape,
                    ModifierKeys.None));

            this.InputBindings.Add(
                new KeyBinding(
                    GalaxyScreenCommands.MapZoomIn,
                    Key.OemPlus,
                    ModifierKeys.Control));

            this.InputBindings.Add(
                new KeyBinding(
                    GalaxyScreenCommands.MapZoomOut,
                    Key.OemMinus,
                    ModifierKeys.Control));

            this.InputBindings.Add(
                new KeyBinding(
                    GalaxyScreenCommands.MapZoomIn,
                    Key.Add,
                    ModifierKeys.Control));

            this.InputBindings.Add(
                new KeyBinding(
                    GalaxyScreenCommands.MapZoomOut,
                    Key.Subtract,
                    ModifierKeys.Control));

            this.InputBindings.Add(
                new KeyBinding(
                    DebugCommands.RevealMap,
                    Key.F12,
                    ModifierKeys.None));

            this.CommandBindings.Add(
                new CommandBinding(
                    ClientCommands.EscapeCommand,
                    ExecuteEscapeCommand,
                    CanExecuteEscapeCommand));
        }

        private void CanExecuteEscapeCommand(object sender, CanExecuteRoutedEventArgs args)
        {
            args.CanExecute = ((this.Model.SelectedTaskForce != null) ||
                               (this.Model.SelectedTradeRoute != null) ||
                               (this.Model.InputMode == GalaxyScreenInputMode.RedeployShips));
        }

        private void ExecuteEscapeCommand(object sender, ExecutedRoutedEventArgs args)
        {
            if (this.Model.SelectedTaskForce != null)
            {
                this.Model.SelectedTaskForce = null;
                args.Handled = true;
            }
            else if (this.Model.SelectedTradeRoute != null)
            {
                this.Model.SelectedTradeRoute = null;
                args.Handled = true;
            }
            else if (this.Model.InputMode == GalaxyScreenInputMode.RedeployShips)
            {
                this.Model.InputMode = GalaxyScreenInputMode.Normal;
                args.Handled = true;
            }
        }

        protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
        {
            if (this.Model.InputMode == GalaxyScreenInputMode.Normal)
            {
                /*
                 * If a task force or trade route is selected, and the user presses the
                 * right mouse button, we only want to cancel the selection--we don't want
                 * to display the pop-up menu in this case.
                 */
                if (this.Model.SelectedTaskForce != null)
                {
                    this.Model.SelectedTaskForce = null;
                    e.Handled = true;
                    CaptureMouse();
                    return;
                }
                if (this.Model.SelectedTradeRoute != null)
                {
                    this.Model.SelectedTradeRoute = null;
                    e.Handled = true;
                    CaptureMouse();
                    return;
                }
            }
            base.OnMouseRightButtonDown(e);
        }

        protected override void OnMouseRightButtonUp(MouseButtonEventArgs e)
        {
            if (IsMouseCaptured)
            {
                ReleaseMouseCapture();
                e.Handled = true;
                return;
            }
            base.OnMouseRightButtonUp(e);
        }
    }
}
